﻿<?php
include_once("../initial.php");
include_once("../driver/drive.php");
include_once("../driver/tableLocation.php");
include_once("../global/global_function.php");
class courseRHandle
{
    public function get_all_data($orderby="", $limit="")
    {
        $tablename = tableLocation::courseTable();
        $sql = "select * from $tablename where `id`!='' $orderby $limit";
        return drive::get_all_rows($sql);
    }
    public function get_course_num($array)
    {
        $tablename = tableLocation::courseTable();
        $sql = get_select_sql($tablename, "count(*)", $array);
        $result = drive::get_one_value($sql);
        return $result;
    }
    public function get_some_data($array, $orderby='', $limit='')
    {
        $tablename = tableLocation::courseTable();
        $sql = "select * from $tablename where ";
        $first=0;
        foreach($array as $key=>$value)
        {
            if($first==0)
            {
                $sql .= "`$key`='$value' ";
                $first=1;
            }
            else
            {
                $sql .= "and `$key`='$value'";
            }
        }
        $sql .= " $orderby $limit";
        return drive::get_all_rows($sql);
    }
    public function get_course_users($conditionArray, $orderby='', $limit='')
		{
				$tablename = tableLocation::userCourseTable();
        $sql = "SELECT * FROM $tablename WHERE ";
        $first=0;
        foreach($conditionArray as $key=>$value)
        {
            if($first==0)
            {
                $sql .= " `$key`='$value' ";
                $first=1;
            }
            else
            {
                $sql .= "and `$key`='$value'";
            }
        }
        $sql .= " $orderby $limit";
        //echo $sql;
        return drive::get_all_rows($sql);
		}
		public function delete_course_user($condition)
    {
    		if($condition=='' || !is_array($condition) || count($condition)<=0)
    		{
    				return false;
    		}
    		$table = tableLocation::userCourseTable();
    		$sqlk=array();
        foreach($condition as $key=>$value)
        {
            array_push($sqlk," `$key`='$value' ");
        }
        $sqls = implode(" and ",$sqlk);
        $sql = "delete from $table where $sqls";
        return drive::exc($sql);
    }
    
    public function get_course_devices($conditionArray, $orderby='', $limit='')
		{
				$tablename = tableLocation::deviceCourseTable();
        $sql = "SELECT * FROM $tablename WHERE ";
        $first=0;
        foreach($conditionArray as $key=>$value)
        {
            if($first==0)
            {
                $sql .= " `$key`='$value' ";
                $first=1;
            }
            else
            {
                $sql .= "and `$key`='$value'";
            }
        }
        $sql .= " $orderby $limit";
        //echo $sql;
        return drive::get_all_rows($sql);
		}
		public function delete_course_device($condition)
    {
    		if($condition=='' || !is_array($condition) || count($condition)<=0)
    		{
    				return false;
    		}
    		$table = tableLocation::userDeviceTable();
    		$sqlk=array();
        foreach($condition as $key=>$value)
        {
            array_push($sqlk," `$key`='$value' ");
        }
        $sqls = implode(" and ",$sqlk);
        $sql = "delete from $table where $sqls";
        return drive::exc($sql);
    }
    public function getCoursesByUserId($uid, $orderby='', $limit='')
    {
    		$courseTable = tableLocation::courseTable();
    		$userCourseTable = tableLocation::userCourseTable();
    		if($orderby=='')
    		{
    				$orderby = 'order by `'.$userCourseTable.'`.`createtime` desc';
    		}
    		$sql = "select * from ".$courseTable." join ".$userCourseTable." 
    						on ".$courseTable.".`id`=".$userCourseTable.".`cid` 
    						where ".$userCourseTable.".`cid`=$uid 
    						$orderby $limit";
    		return drive::get_all_rows($sql);
    }
    
    public function searchCourse($keyword, $limit='')
    {
    		$table = tableLocation::courseTable();
    		$sql = "select * from $table where name like '%$keyword%' or teacherid like '%$keyword%' order by id desc $limit";
    		return drive::get_all_rows($sql);
    }
}
?>